<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="1024" @ok="handleSubmit">
      <BasicForm @register="registerForm" ref="formRef">
        <template #billTypeSlot="{ model, field }">
          <a-input-search :disabled="isUpdate" v-model:value="model[field]" placeholder="请选择单据类型" @search="openBillType" />
        </template>
      </BasicForm>
  <!-- 子表单区域 -->
    <a-tabs v-model:activeKey="activeKey" animated @change="handleChangeTabs">
      <a-tab-pane tab="要货计划子表" key="basRequisitionPlanSon" :forceRender="true">
        <JVxeTable
          keep-source
          resizable
          ref="basRequisitionPlanSon"
          :loading="basRequisitionPlanSonTable.loading"
          :columns="basRequisitionPlanSonTable.columns"
          :dataSource="basRequisitionPlanSonTable.dataSource"
          :height="340"
          :rowNumber="true"
          :rowSelection="true"
          :disabled="formDisabled"
          :toolbar="true"
          >
          <template #productName="props">
            <a-input-search @search="handleView(props)" placeholder="请选择产品编码" v-model:value="props.row.productCode"></a-input-search>
          </template>
        </JVxeTable>
      </a-tab-pane>
    </a-tabs>
  </BasicModal>
  <!-- 料品 -->
  <materialSelectModel @register="materialRegModal" @getSelectResult="materialhandleSuccess" v-bind="getBindValue"></materialSelectModel>
  <!-- 单据类型 -->
  <billTypeSelectModel @register="billTypeRegModal" @getSelectResult="billTypehandleSuccess" v-bind="getBindValue"></billTypeSelectModel>
  
</template>

<script lang="ts" setup>
    import {ref, computed, unref,reactive} from 'vue';
    import {BasicModal, useModalInner,useModal} from '/@/components/Modal';
    import {BasicForm, useForm} from '/@/components/Form/index';
    import { JVxeTable } from '/@/components/jeecg/JVxeTable'
    import { useJvxeMethod } from '/@/hooks/system/useJvxeMethods.ts'
    import {formSchema,basRequisitionPlanSonColumns} from '../BasRequisitionPlan.data';
    import {saveOrUpdate,basRequisitionPlanSonList} from '../BasRequisitionPlan.api';
    import { VALIDATE_FAILED } from '/@/utils/common/vxeUtils';
    import materialSelectModel from '/@/views/components/materialSelectModel.vue';
    import billTypeSelectModel from '/@/views/components/billTypeSelectModel.vue';

    // Emits声明
    const emit = defineEmits(['register','success']);
    const isUpdate = ref(true);
    const formDisabled = ref(false);
    const refKeys = ref(['basRequisitionPlanSon', ]);
    const activeKey = ref('basRequisitionPlanSon');
    const basRequisitionPlanSon = ref();
    const tableRefs = {basRequisitionPlanSon, };
    const basRequisitionPlanSonTable = reactive({
          loading: false,
          dataSource: [],
          columns:basRequisitionPlanSonColumns
    })
    //表单配置
    const [registerForm, {setProps,resetFields, setFieldsValue, validate}] = useForm({
        labelWidth: 100,
        schemas: formSchema,
        showActionButtonGroup: false,
        baseColProps: {span: 8}
    });
     //表单赋值
    const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
        //重置表单
        await reset();
        setModalProps({confirmLoading: false,showCancelBtn:data?.showFooter,showOkBtn:data?.showFooter});
        isUpdate.value = !!data?.isUpdate;
        formDisabled.value = !data?.showFooter;
        if (unref(isUpdate)) {
            //表单赋值
            await setFieldsValue({
                ...data.record,
            });
             requestSubTableData(basRequisitionPlanSonList, {id:data?.record?.id}, basRequisitionPlanSonTable)
        }
        // 隐藏底部时禁用整个表单
       setProps({ disabled: !data?.showFooter })
    });
    //方法配置
    const [handleChangeTabs,handleSubmit,requestSubTableData,formRef] = useJvxeMethod(requestAddOrEdit,classifyIntoFormData,tableRefs,activeKey,refKeys);

    //设置标题
    const title = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
    const [billTypeRegModal, { openModal: openBillTypeModal }] = useModal();

    //打开选择单据类型
    function openBillType() {
      openBillTypeModal(true, {
        isUpdate: false,
        showFooter: true,
      });
    }
    async function reset(){
      await resetFields();
      activeKey.value = 'basRequisitionPlanSon';
      basRequisitionPlanSonTable.dataSource = [];
    }
    function classifyIntoFormData(allValues) {
         let main = Object.assign({}, allValues.formValue)
         return {
           ...main, // 展开
           basRequisitionPlanSonList: allValues.tablesValue[0].tableData,
         }
       }
       let index: number | null = null;
      //打开选择料品
      function handleView(props) {
        index = props.params.$rowIndex;
        openMaterialModal(true, {
          isUpdate: false,
          showFooter: true,
        });
      }
    //表单提交事件
    async function requestAddOrEdit(values) {
        try {
            setModalProps({confirmLoading: true});
            //提交表单
            await saveOrUpdate(values, isUpdate.value);
            //关闭弹窗
            closeModal();
            //刷新列表
            emit('success');
        } finally {
            setModalProps({confirmLoading: false});
        }
    }
    // 注册model
    const [materialRegModal, { openModal: openMaterialModal }] = useModal();
    const getBindValue = Object.assign({}, '');
    
    //料品回写
    function materialhandleSuccess(val, values) {
      basRequisitionPlanSonTable.dataSource = basRequisitionPlanSon.value!.getTableData();
      basRequisitionPlanSonTable.dataSource[index].productName = val[0].label;
      basRequisitionPlanSonTable.dataSource[index].productId = val[0].value;
      basRequisitionPlanSonTable.dataSource[index].productCode = val[0].item.code;
      
    }
</script>

<style lang="less" scoped>
	/** 时间和数字输入框样式 */
    :deep(.ant-input-number){
		width: 100%
	}

	:deep(.ant-calendar-picker){
		width: 100%
	}
</style>